/* TELNET.C
 * Copyright (c) 2007 MissingNODE 
 * Based on GPL OpenQM Code
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 * 
 * START-HISTORY:
 * Oct-10-2007 Initial Revision
 * END-HISTORY
 *
 * START-DESCRIPTION:
 *
 * **********************************************************************
 * NOTE: THIS CODE IS REPLICATED BY A SIMILAR FUNCTION IN TMSVC.C
 * CHANGES MAY NEED TO BE APPLIED IN BOTH MODULES
 * **********************************************************************
 *
 * END-DESCRIPTION
 *
 * START-CODE
 */

#include <tm.h>
#include <tio.h>
#include <telnet.h>

char socket_byte(void);

/* ======================================================================
   Negotiate telnet parameter

   Returns true if parameter processed, false if was real IAC data char  */

bool negotiate_telnet_parameter()
{
 unsigned char c;
 
 c = (unsigned char)socket_byte();
 if (c == TN_IAC) return FALSE;     /* Treat as real IAC character */

 switch((unsigned char)c)   /* Process negotiation text */
  {
   case TN_IP:     /* Interrupt process */
   case TN_BRK:    /* Break key */
      break_key();
      break;

   case TN_WILL:   /* WILL: Acknowledgement of DO */
      c = (unsigned char)socket_byte();
      switch(c)
       {
        case 0x00:   /* WILL Binary mode */
           telnet_binary_mode_in = TRUE;
           to_outbuf("\xFF\xFD\x00", 3); /* DO Binary mode */
           break;
       }
      break;

   case TN_DO:     /* DO: Sender wants to enable a feature */
      c = (unsigned char)socket_byte();
      switch(c)
       {
        case 0x00:   /* DO Binary mode */
           telnet_binary_mode_out = TRUE;
           to_outbuf("\xFF\xFB\x00", 3); /* WILL Binary mode */
           break;
       }
      break;

   case TN_WONT:   /* WONT: Rejection of DO, acknowledgement of DONT */
      c = (unsigned char)socket_byte();
      switch(c)
       {
        case 0x00:   /* DONT Binary mode */
           telnet_binary_mode_in = FALSE;
           to_outbuf("\xFF\xFE\x00", 3); /* DONT Binary mode */
           break;
       }
      break;

   case TN_DONT:   /* DONT: Sender wants to disable a feature */
      c = (unsigned char)socket_byte();
      switch(c)
       {
        case 0x00:   /* DONT Binary mode */
           telnet_binary_mode_out = FALSE;
           to_outbuf("\xFF\xFC\x00", 3); /* WONT Binary mode */
           break;
       }
      break;

   case TN_SB:     /* SB: Subnegotiation */
      break;
  }

 flush_outbuf();

 return TRUE;
}


/* END-CODE */
